package com.kailu.hotel.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.kailu.hotel.model.ServiceModel;
import com.kailu.hotel.model.ServiceUsingModel;
import com.kailu.hotel.service.ServiceManager;
import com.kailu.hotel.service.ServiceUsingManger;

/**
 * @author viet.ch
 * @generated May 3, 2014
 * @from VN
 */

@Controller
public class ServiceUsingController {

	@Autowired
	ServiceUsingManger suManager;
	@Autowired
	ServiceManager serviceManager;
	
	@RequestMapping(value = "/roomusing/addservice", method = RequestMethod.POST)
    public String addRoom(@ModelAttribute(value="serviceusing") ServiceUsingModel service, BindingResult result)
    {
		ServiceModel serviceModel = serviceManager.getServiceById(service.getService_id());
		int number = serviceModel.getService_number();
		
		if ((number - service.getNumber()) < 0 ) {
			return "redirect:/roomusing/"+service.getRoom_id()+"/fail";
		} else {
			serviceModel.setService_number(number - service.getNumber());
			serviceManager.updateService(serviceModel);
			suManager.addServiceUsing(service);
			return "redirect:/roomusing/"+service.getRoom_id();
		}
    }
	
}
